/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution567 {

    public static void main(String args[]){
        Solution567 s = new Solution567();
        System.out.println(s.checkInclusion("ab","bahbhaas"));
    }

    public boolean checkInclusion(String s1, String s2) {
        int[] mp1 = new int[26];
        for(int i = 0 ; i < s1.length() ; i ++){
            mp1[s1.charAt(i) - 'a'] ++;
        }

        int l = 0;
        int r = 0;
        int[] mp2 = new int[26];
        int cnt = 0;
        while(r < s2.length()){
            int idx = s2.charAt(r) - 'a';
            mp2[idx] ++;
            cnt ++;
            if(mp2[idx] > mp1[idx]){
                while(l < s2.length() && mp2[idx] > mp1[idx]){
                    mp2[s2.charAt(l) - 'a'] --;
                    cnt --;
                    l ++;
                }
            }
            if(cnt == s1.length()){
                return true;
            }
            r ++;
        }
        return false;
    }

}
